package com.fling.background;

import java.util.ArrayList;
import java.util.Arrays;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.fling.Constants;
import com.fling.Locator;

public class BackgroundBlockRow implements Constants {

	private ArrayList<BackgroundBlock> row;
	private ArrayList<BackgroundBlock> deleted;

	public BackgroundBlockRow() {
		row = new ArrayList<BackgroundBlock>();
		deleted = new ArrayList<BackgroundBlock>();
	}

	public void makeRow(float y_pos) {
		BackgroundBlock block = null;
		ArrayList<Integer> positions = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7));
		int numOfBlocks = MathUtils.random(3, 7);
		for (int i = 0; i < numOfBlocks; i++) {
			int x_pos = MathUtils.random(0, positions.size() - 1);
			x_pos = positions.remove(x_pos);
			block = new BackgroundBlock(new Vector2(WALL_WIDTH * Locator.getxSpriteScale() + Locator.getBlockSpacing() * x_pos / 2, VIRTUAL_HEIGHT
					* Locator.getySpriteScale() + (BLOCK_HEIGHT / 2) * Locator.getySpriteScale() * y_pos));
			row.add(block);
		}
	}

	public void render(SpriteBatch sb) {
		for (BackgroundBlock b : row) {
			b.render(sb, 0.3f);
		}
	}

	public void update(float delta) {

		for (BackgroundBlock b : row) {
			b.update(delta);
			if (!b.isExists()) {
				deleted.add(b);
			}
		}
		row.removeAll(deleted);
		deleted.clear();
	}

	public ArrayList<BackgroundBlock> getRow() {
		return row;
	}

	public void setRow(ArrayList<BackgroundBlock> row) {
		this.row = row;
	}

}
